package com.office.automate.servlet.employee;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.office.automate.exception.framework.FrontierVisibleException;
import com.office.automate.frontier.utility.DataConversionUtility;
import com.office.automate.model.employee.Employee;
import com.office.automate.service.impl.employee.EmployeeServiceImplementer;

/**
 * Servlet implementation class EmployeeImageServlet
 */
@WebServlet("/EmployeeImageServlet")
public class EmployeeImageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private DataConversionUtility dcUtility = new DataConversionUtility();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EmployeeImageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		try {
			long employeeId = dcUtility.convertStringToLong(request.getParameter("employeeId"));
			Employee employee = new EmployeeServiceImplementer().getEmployeeDetails(employeeId);
			byte[] img =  employee.getImage();
		    response.setContentType("image/jpg");
		    response.setContentLength(img.length);
		    response.getOutputStream().write(img);
		    response.getOutputStream().flush();
		    response.getOutputStream().close();
		} catch (FrontierVisibleException e) {
			e.printStackTrace();
		}
	}
}
